package com.xuecheng.teaching.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.commons.constants.WorkConstans;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.WorkRecordDto;
import com.xuecheng.commons.model.vo.*;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.teaching.domain.CourseWorkRec;
import com.xuecheng.teaching.domain.Work;
import com.xuecheng.teaching.domain.WorkRecord;
import com.xuecheng.teaching.mapper.WorkRecordMapper;
import com.xuecheng.teaching.service.CourseWorkRecService;
import com.xuecheng.teaching.service.WorkRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.teaching.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 作业提交记录 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-08-13
 */
@Service
public class WorkRecordServiceImpl extends ServiceImpl<WorkRecordMapper, WorkRecord> implements WorkRecordService {

    @Autowired
    private CourseWorkRecService courseWorkRecService;


    @Autowired
    private WorkRecordService workRecordService;


    @Autowired
    private WorkService workService;

    //  添加或修改作业记录--学员
    @Override
    public ResponseResult saveOrUpdateRecord(WorkRecordDto workRecordDto) {

        //  查询作业信息
        Work work = workService.getById(workRecordDto.getWorkId());

        //  封装作业提交记录对象
        WorkRecord workRecord = BeanHelper.copyProperties(work, WorkRecord.class);

        //  补充字段信息
        workRecord.setUserId(AuthInfoHolder.getUserId());   //  作业提交人id
        workRecord.setUsername("1"); //  todo 作业提交人
        workRecord.setWorkId(workRecordDto.getWorkId());    //  作业id
        workRecord.setCoursePubId(workRecordDto.getCoursePubId());  //  课程发布id
        //workRecord.setTeachplanName(workRecordDto.getTeachplanName());  //  课程计划名
        //workRecord.setQuestion(workRecordDto.getQuestion());  //  内容
        workRecord.setType(workRecordDto.getType());  //  类型
        workRecord.setStatus(WorkConstans.RECORD_CORRECT_PENDING);  //  状态
        workRecord.setAnswer(workRecordDto.getAnswer());    //  完成内容
        workRecord.setTeacherId(1L);    //  TODO 批改人id
        workRecord.setCorrectComment(workRecordDto.getCorrectComment());    //  批改意见
        workRecord.setCorrectionDate(workRecordDto.getCorrectionDate());    //  批改时间
//        workRecord.setCreateDate(workRecordDto.getCreateDate());    //  创建时间
        workRecord.setChangeDate(DateUtil.date());

        //  更新或保存作业提交记录
        workRecordService.saveOrUpdate(workRecord);

        //  构建条件
        LambdaQueryWrapper<CourseWorkRec> qw = new LambdaQueryWrapper<>();
        qw.eq(CourseWorkRec::getCoursePubId,workRecord.getCoursePubId());
        CourseWorkRec courseWorkRec = courseWorkRecService.getOne(qw);
        //  判断是否存在作业记录
        if (ObjectUtil.isNotEmpty(courseWorkRec)){  //  存在就更新
            //  构建条件
            LambdaQueryWrapper<WorkRecord> wq = new LambdaQueryWrapper<>();
            wq.eq(WorkRecord::getUserId,AuthInfoHolder.getUserId());
            wq.eq(WorkRecord::getUsername,workRecord.getUsername());
            wq.eq(WorkRecord::getWorkId,workRecord.getWorkId());
            WorkRecord one = workRecordService.getOne(wq);
            //  判断是否为同一用户
            if (ObjectUtil.isNotEmpty(one)){    //  存在就修改提交时间
                courseWorkRec.setCommitedTime(DateUtil.date());
                courseWorkRecService.updateById(courseWorkRec);
            }else{  //  不存在就修改提交的作业数量，答题人数，待批阅数
                courseWorkRec.setTobeReviewed(courseWorkRec.getTobeReviewed() + 1); //  待批阅数
                courseWorkRec.setAnswerNumber(courseWorkRec.getAnswerNumber() + 1); //  答题人数
                courseWorkRec.setWorkNumber(courseWorkRec.getWorkNumber() + 1); //  作业总数
                courseWorkRecService.updateById(courseWorkRec);
            }
        }else{  //  不存在就新增
            CourseWorkRec courseWorkRec1 = new CourseWorkRec();
            courseWorkRec1.setCoursePubId(workRecordDto.getCoursePubId());
            courseWorkRec1.setCompanyName(work.getCompanyName());
            courseWorkRec1.setCourseName(work.getCourseName());
            courseWorkRec1.setCompanyId(work.getCompanyId());
            courseWorkRec1.setCreateDate(DateUtil.date());
            courseWorkRec1.setCommitedTime(DateUtil.date());
            courseWorkRec1.setId(work.getId());
            courseWorkRec1.setTobeReviewed(1);
            courseWorkRec1.setAnswerNumber(1);
            courseWorkRec1.setWorkNumber(1);
            courseWorkRecService.save(courseWorkRec1);
        }
        //  返回结果
        return ResponseResult.okResult();
    }


    //  批阅详情-作业列表
    @Override
    public ResponseResult workPage(PageRequest params, Long teachplanId) {
        //  1.构建条件
        LambdaQueryWrapper<WorkRecord> qw = new LambdaQueryWrapper<>();
        //  2.构建分页
        Page<WorkRecord> page = new Page<>(params.getPageNo(), params.getPageSize());
        //  3.分页查询
        page = this.page(page, qw);

        List<WorkRecord> workRecordList = page.getRecords();

        List<WorkListVo> voList = new ArrayList<>();
        for (WorkRecord workRecord : workRecordList) {
            WorkListVo workListVo = BeanHelper.copyProperties(workRecord, WorkListVo.class);
            //补全信息
            workListVo.setWorkRecordId(workRecord.getId());
            workListVo.setCreateDate(DateUtil.formatDateTime(workRecord.getCreateDate()));
            voList.add(workListVo);
        }


        //  4.返回分页结果
        return PageResponseResult.okResult(page.getTotal(), voList);
    }
}
